//
//  XMSecondController.m
//  Second
//
//  Created by Alex on 16/7/14.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "XMSecondController.h"

@interface XMSecondController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *iwantyou;
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *context;
@property (weak, nonatomic) IBOutlet UILabel *noplace;
@property (weak, nonatomic) IBOutlet UILabel *place;
@property (weak, nonatomic) IBOutlet UILabel *highSchool;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *datasource;
@property (nonatomic,strong)NSMutableArray *classDatasource;
@property (nonatomic,strong)UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIView *line;
@property (nonatomic,assign)NSInteger number;
@property (nonatomic,strong)NSArray *imageArray;
@end

@implementation XMSecondController

- (NSMutableArray *)datasource {
    
    if (!_datasource) {
        
        _datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (NSMutableArray *)classDatasource {
    
    if (!_classDatasource) {
        
        _classDatasource = [NSMutableArray array];
    }
    return _classDatasource;
}

- (NSArray *)imageArray {
    
    if (!_imageArray) {
        
        _imageArray = [NSArray array];
    }
    return _imageArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"商品详情";
    self.headerImage.layer.masksToBounds = YES;
    self.headerImage.layer.cornerRadius = 15;
  
    
        self.name.text = _model.user_name;
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:_model.head_image]placeholderImage:[UIImage imageNamed:@"tabBar_friendTrends_icon"]];
        self.noplace.text = [NSString stringWithFormat:@"¥%@",_model.price];
        self.place.text = [NSString stringWithFormat:@"¥%@",_model.current_price];
        self.highSchool.text = _model.school;
        self.context.text = _model.detail;
    
    
    NSArray *VCarray = [self.navigationController viewControllers];
    NSInteger vccount = VCarray.count;
    UIViewController *lastVC = VCarray[vccount - 2];
    if ([lastVC isKindOfClass:[XMClassListViewController class]]) {
        
        [RequestManager fecthDataWithUrl:@"http://youzixy.com/app/sale/goodsdis" parameter:@{@"goodsid":self.saleid} mehtod:@"GET" compelet:^(NSData *data) {
            
            NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            self.name.text = bigdic[@"username"];
            [self.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.youzixy.com/Uploads/heads/%@",bigdic[@"headpic"]]]];
            
            self.place.text = [NSString stringWithFormat:@"¥%@",bigdic[@"price"]];
            self.highSchool.text = bigdic[@"schoolname"];
            self.context.text = bigdic[@"detail"];
            NSRange range = [self.context.text rangeOfString:@"(联系我的时候，请说明是在柚子校园看见的噢！)"];
            NSMutableString *string = [NSMutableString stringWithString:self.context.text];
            [string deleteCharactersInRange:range];
            self.context.text = string;            
            self.noplace.hidden = YES;
            self.line.hidden = YES;
            self.imageArray = @[[NSString stringWithFormat:@"http://www.youzixy.com/Uploads/%@",bigdic[@"image1"]],[NSString stringWithFormat:@"http://www.youzixy.com/Uploads/%@",bigdic[@"image2"]],[NSString stringWithFormat:@"http://www.youzixy.com/Uploads/%@",bigdic[@"image3"]],[NSString stringWithFormat:@"http://www.youzixy.com/Uploads/%@",bigdic[@"image4"]]];
            self.classDatasource = @[].mutableCopy;
            
        } updateUI:^{
            
            
            [self.collectionView reloadData];
            
        }];
        
    }
    
 
   
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView.collectionViewLayout = layout;
    layout.itemSize = CGSizeMake((kWidth-50)/3,(kWidth-50)/3);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.collectionView registerNib:[UINib nibWithNibName:@"XMImageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"image_cell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    NSArray *VCarray = [self.navigationController viewControllers];
    NSInteger vccount = VCarray.count;
    UIViewController *lastVC = VCarray[vccount - 2];
    if ([lastVC isKindOfClass:[XMClassListViewController class]]) {
    
        return self.imageArray.count;
    } else {
    
      return self.model.images.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XMImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"image_cell" forIndexPath:indexPath];
    
    
    NSArray *VCarray = [self.navigationController viewControllers];
    NSInteger vccount = VCarray.count;
    UIViewController *lastVC = VCarray[vccount - 2];
    if ([lastVC isKindOfClass:[XMClassListViewController class]]) {
    
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[indexPath.row]]];
      
    
    } else {
        
         [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.model.images[indexPath.row]]];
        
    }
    
   
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XMImageCollectionViewCell *cell = (XMImageCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [ZoomImage showImage:cell.imageView];
    
    
}

- (IBAction)iwantyou:(UIButton *)sender {
    
    NSArray *VCarray = [self.navigationController viewControllers];
    NSInteger vccount = VCarray.count;
    UIViewController *lastVC = VCarray[vccount - 2];
    if ([lastVC isKindOfClass:[XMClassListViewController class]]) {
    
        XMWantController *want = [[XMWantController alloc]init];
        want.place = self.place.text;
        want.himage = self.imageArray[0];
        
        [self.navigationController pushViewController:want animated:YES];
    }else {
        
    XMWantController *want = [[XMWantController alloc]init];
    want.price = self.model.price;
        want.heimage = self.model.images[0];
  
    [self.navigationController pushViewController:want animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
